/**
 * create by young (young2f@outlook.com) on 2020/9/16
 */


'use strict';
import React from 'react';
import {Button, Form, notification} from 'antd';

import {FormLayout, ModalFooter} from 'antd-extra';
import FormBase from '../../../components/Base/FormBase';
import {connect} from "dva";
import moment from 'moment';
import 'moment/locale/zh-cn';

moment.locale('zh-cn');
const table = 'Material';
@Form.create()
@connect(({
            user,
            global,
            dynamicLabel,
            material,
            treeNode
          }) => ({...user, ...material, ...dynamicLabel, ...global, ...treeNode}))
export default class ModalCreateOrEditMaterial extends FormBase {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
  }


  renderFoot = () => {
    const {foot, value, modalRef, callback, form} = this.props;

    const model = this.props[`${table}Model`];
    if (foot) {
      return foot(value, modalRef);
    } else {
      return (
        <ModalFooter>
          <Button type="primary" onClick={() => {
            form.validateFields((err, values) => {
              if (err) {
                const {code} = err;
                const {errors} = code;
                errors.forEach(item => {
                  notification.error(item);
                });
                return;
              }

              callback(values);
              modalRef.close();
            });
          }}>确定</Button>
          <Button onClick={() => {
            modalRef.close();
          }}>取消</Button>
        </ModalFooter>
      );
    }
  };


  render() {
    const {form, footDisabled, style, values, cols = 2} = this.props;
    const model = this.props[`${table}Model`];
    _.map(model, (item, key) => {
      item.key = key;
    });

    return (
      <div style={{...style}}>
        {<FormLayout form={form} cols={cols}>
          {this.getDicFields(model, values)}
        </FormLayout>}
        {!footDisabled && this.renderFoot()}
      </div>
    );
  }
}

